package com.summer.test.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.summer.core.annotations.ScopeType;
import com.summer.core.controller.EntityHome;
import com.summer.core.controller.LoginUserInfo;
import com.summer.core.util.FacesUtil;
import com.summer.core.util.IdGenerator;
import com.summer.test.model.SystemLoanFee;
import com.summer.user.model.User;

@Component
@Scope(ScopeType.VIEW)
public class LoanFeeHome extends EntityHome<SystemLoanFee> {

	@Resource
	HibernateTemplate ht;

	@Resource
	LoginUserInfo loginUserInfo;

	/**
	 * 添加/修改产品费用配置信息
	 */

	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public String save() {

		try {
			String loginUserId = loginUserInfo.getLoginUserId();
			User user = getBaseService().get(User.class, loginUserId);
			if (StringUtils.isEmpty(getInstance().getId())) {
				getInstance().setId(IdGenerator.randomUUID());
				getInstance().setOperUser(user);
				getInstance().setOperDatetime(new Date());

				getInstance().setUpdateDatetime(new Date());
				getInstance().setUpdateUser(user);
				getBaseService().save(getInstance());
				FacesUtil.addInfoMessage("保存成功！");

			} else {
				getInstance().setUpdateDatetime(new Date());
				getInstance().setUpdateUser(user);
				getBaseService().merge(getInstance());
				FacesUtil.addInfoMessage("修改成功！");
			}

			return FacesUtil.redirect("/admin/test/systemLoanFee");
		} catch (DataAccessException e) {
			e.printStackTrace();
			FacesUtil.addErrorMessage("保存失败！");
		}
		return null;
	}

	@Override
	@Transactional(readOnly = false, rollbackFor = Exception.class)
	public String delete(String id) {
		// TODO Auto-generated method stub
		return super.delete(id);
	}

}
